package com.oc.mba.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.oc.mba.dao.OcPaperDAO;
import com.oc.mba.dao.OcPaperQuestionDAO;
import com.oc.mba.dao.OcQuestionDAO;
import com.oc.mba.dao.OcQuestionOptionDAO;
import com.oc.mba.dao.OcTopicDAO;
import com.oc.mba.dao.QuestionSeckillDAO;
import com.oc.mba.dao.QuestionSolutionDAO;
import com.oc.mba.dao.impl.OcQuestionDAOImpl;
import com.oc.mba.entity.OcPaper;
import com.oc.mba.entity.OcPaperQuestion;
import com.oc.mba.entity.OcQuestion;
import com.oc.mba.entity.OcQuestionOption;
import com.oc.mba.entity.OcQuestionSeckill;
import com.oc.mba.entity.OcQuestionSolution;
import com.oc.mba.entity.PagingModel;
import com.oc.mba.service.PaperService;
import com.oc.mba.util.CommConsts;
import com.oc.mba.util.CommLog;
import com.oc.mba.util.PaginationSupport;

@Service
public class PaperServiceImpl implements PaperService {

	private static final CommLog logger = new CommLog(PaperServiceImpl.class);
	
	@Autowired
	private OcPaperDAO paperDAO;
	
	@Autowired
	private OcTopicDAO topicDAO;
	
	@Autowired
	private OcQuestionDAO questionDAO;
	
	@Autowired
	private OcPaperQuestionDAO paperQuestionDAO;
	
	@Autowired
	private OcQuestionOptionDAO questionOptionDAO;
	
	@Autowired
	private QuestionSolutionDAO questionSolutionDAO;
	
	@Autowired
	private QuestionSeckillDAO questionSeckillDAO;
	
	@Override
	public List<OcPaper> list() {
		return paperDAO.list();
	}

	@Override
	public OcPaper findPaper(Integer paperID) {
		return paperDAO.findPaper(paperID);
	}

	@Override
	public List<OcPaper> listAll() {
		return paperDAO.listAll();
	}

	@Override
	public void save(OcPaper paper) {
		paperDAO.save(paper);
	}

	@Override
	public void deletePaper(Integer paperID) {
		paperDAO.deletePaper(paperID);
	}

	@Override
	public void update(OcPaper paper) {
		paperDAO.update(paper);
	}

	@Override
	public List<OcQuestion> findQuestionByPaperID(Integer paperID) {
		OcPaper paper = paperDAO.findPaper(paperID);
		if (paper != null) {
			Set<OcPaperQuestion> paperQuestions = paper.getOcPaperQuestions();
			List<Integer> questionIDs = new ArrayList<Integer>();
			Iterator<OcPaperQuestion> it = paperQuestions.iterator();
			while (it.hasNext()) {
				OcPaperQuestion paperQuestion = it.next();
				questionIDs.add(paperQuestion.getQuestionId());
			}
			if (questionIDs.size() == 0) {
				return new ArrayList<OcQuestion>();
			}
			return questionDAO.findQuestionByIDs(questionIDs);
		}
		return null;
	}

	@Override
	public List<OcQuestion> findQuestionBySerial(String serial) {
		return questionDAO.findQuestionBySerial(serial);
	}

	@Override
	public void savePaperQuestion(OcPaperQuestion paperQuestion) {
		paperQuestionDAO.save(paperQuestion);
	}

	@Override
	public OcPaperQuestion findPaperQuestion(Integer paperID, Integer questionID) {
		return paperQuestionDAO.findPaperQuestion(paperID, questionID);
	}

	@Override
	public void removePaperQuestion(Integer paperID, Integer questionID) {
		paperQuestionDAO.removePaperQuestion(paperID, questionID);
	}


	@Override
	public PaginationSupport listAllQuestionByPaging(int current) {
		return questionDAO.listAllPublishQuestionForPaging(Integer.valueOf(CommConsts.PAGESIZE), current);
	}

	@Override
	public void save(OcQuestion question) {
		questionDAO.save(question);
	}

	@Override
	public OcQuestion findQuestion(int questionID) {
		return questionDAO.findQuestion(questionID);
	}

	@Override
	public void publishQuestion(int questionID) {
		OcQuestion question = findQuestion(questionID);
		if (question != null) {
			question.setQuestionStatus((short)1);
			questionDAO.update(question);
		}
	}

	@Override
	public void unpublishQuestion(int questionID) {
		OcQuestion question = findQuestion(questionID);
		if (question != null) {
			question.setQuestionStatus((short)0);
			questionDAO.update(question);
		}
	}

	@Override
	public void deleteQuestion(int questionID, boolean real) {
		questionDAO.deleteQuestion(questionID, real);
	}

	@Override
	public void save(OcQuestionOption option) {
		questionOptionDAO.save(option);
	}

	@Override
	public void deleteQuestionOption(Integer optionID) {
		questionOptionDAO.deleteOption(optionID);
	}

	@Override
	public void save(OcQuestionSolution solution) {
		questionSolutionDAO.save(solution);
	}

	@Override
	public void save(OcQuestionSeckill seckill) {
		questionSeckillDAO.save(seckill);
	}

	@Override
	public void deleteQuestionSolution(Integer solutionID) {
		questionSolutionDAO.deleteSolution(solutionID);
	}

	@Override
	public void deleteQuestionSeckill(Integer seckillID) {
		questionSeckillDAO.deleteSeckill(seckillID);
		
	}

	@Override
	public void updateQuestionOptionFault(Integer optionID) {
		questionOptionDAO.updateQuestionOptionFault(optionID);
	}
	
	@Override
	public void updateQuestionOptionCorrect(Integer optionID) {
		questionOptionDAO.updateQuestionOptionCorrect(optionID);
	}

}
